<?php
namespace Home\Controller;
use Think\Controller;
use Home\Model\CategoryModel;

class IndexBaseController extends Controller {
	public function _initialize(){
		//处理自动登入   用户没有登入的状态session，但是有自动登入的cookie
		if(isset($_COOKIE['auto_login']) && !isset($_SESSION['uid'])){
			$value = explode('|', encryption($_COOKIE['auto_login'], 1));
			$ip = get_client_ip();
			//本次登入ip与上一次登入ip是否相同
			if($ip == $value[1]){
				$username = $value[0];
				$user = M('user') -> where("uname = '$username'") -> field('uid, uname, lock_status') -> find();
				//查询改用户信息，并且没有被锁定，保存登入状态
				if($user && !$user['lock_status']){
					$_SESSION['uid'] = $user['uid'];
					$_SESSION['username'] = $user['uname'];
				}
			}
		}
		if(!isset($_SESSION['uid'])){
			$this -> error('请先登入', U('Login/index'), 2);
		}
		//var_dump($_SESSION);
		//var_dump($_COOKIE);
		$this -> assign('username', $_SESSION['username']);
		$this -> setNav();
		
	}
	
	protected function setNav(){
		$category_model = new CategoryModel();
		$nav = $category_model -> getCategoryLevel(0);
		$this -> assign('nav', $nav);
	}
}